package com.onpositive.gamemaker.treeview;

import java.io.File;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.part.ViewPart;

import com.onpositive.gamemaker.editor.EditorUtil;

public class DirectoryView extends ViewPart
{

	public static final String ID = "com.onpositive.gamemaker.editor.directoryView";
	protected TreeViewer treeViewer;
	protected String initialLocation;
	protected String location;
	

	
	public void createPartControl(Composite parent)
	{
		Composite composite = new Composite(parent, SWT.NONE);
	    composite.setLayout(new GridLayout(1, false));

	    treeViewer = new TreeViewer(composite);
	    Tree tree = treeViewer.getTree();
	    tree.setLayoutData(new GridData(GridData.FILL_BOTH));
	    treeViewer.setContentProvider(new FileTreeContentProvider());
	    treeViewer.setLabelProvider(new FileTreeLabelProvider());
	    treeViewer.setInput(getInitialLocation()); // pass a non-null that will be ignored
	    new DirViewerTooltipSupport(tree, ToolTip.NO_RECREATE, false);
	    //support.activate();
	    
	    treeViewer.addOpenListener(new IOpenListener()
		{
			
			
			public void open(OpenEvent event)
			{
				ISelection selection = event.getSelection();
				if (selection instanceof IStructuredSelection)
				{
					Object firstElement = ((IStructuredSelection) selection).getFirstElement();
					if (firstElement instanceof File)
					{
						if (((File) firstElement).exists())
							EditorUtil.openEditorForFile((File) firstElement,null);
						else
						{
							MessageDialog.open(MessageDialog.WARNING,Display.getDefault().getActiveShell(),"Can't open file","Selected file " + ((File) firstElement).getName() + " no longer exists", SWT.NONE);
							//update directory structure
							treeViewer.setInput(location);
						}
					}
				}
				
			}
		});   

	}
	
	public void setWorkspaceLocation(String location)
	{
		treeViewer.setInput(location);
		this.location = location;
	}
	
	protected String getInitialLocation()
	{
		if (initialLocation != null)
			return initialLocation;
		return Platform.getLocation().toOSString();
	}

	
	public void setFocus()
	{
		// TODO Auto-generated method stub

	}

	/**
	 * @param initialLocation Initial working location to set
	 */
	public void setInitialLocation(String initialLocation)
	{
		this.initialLocation = initialLocation;
		this.location = initialLocation;
		if (treeViewer != null)
			treeViewer.setInput(initialLocation);
	}

	/**
	 * @return Current workspace location
	 */
	public String getWorkspaceLocation()
	{
		return location;
	}

}
